package com.youth.executor;

import com.youth.entity.po.Commodity;
import com.youth.utils.ExcelReaderUtil;
import lombok.extern.slf4j.Slf4j;


import org.apache.poi.ss.usermodel.Sheet;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * @ClassName ExcelReaderThread
 * @Description: TODO
 * @Author yjwu
 * @Date 2019/12/7 16:38
 **/
@Slf4j
public class ExcelReaderThread implements Callable {
    private String beanName;
    private CountDownLatch countDownLatch;
    private Sheet sheet;
    private int benginRowNumber;
    private int endRowNumber;
    private Class clazz;


    public ExcelReaderThread(String beanName, CountDownLatch countDownLatch, Sheet sheet, int benginRowNumber, int endRowNumber, Class clazz) {
        this.beanName = beanName;
        this.countDownLatch = countDownLatch;
        this.sheet = sheet;
        this.benginRowNumber = benginRowNumber;
        this.endRowNumber = endRowNumber;
        this.clazz = clazz;
    }

    @Override
    public List<Object> call() throws Exception {
        List<Object> result = new ArrayList<>();
        log.info("ExcelReaderThread {} start,benginRowNumber {},endRowNumber {},countDownLatch {}", Thread.currentThread().getName(), benginRowNumber, endRowNumber, countDownLatch.getCount());
        for (int i = benginRowNumber; i <= endRowNumber; i++) {
            if (i != 0 && i != sheet.getLastRowNum()) {
                Object o = ExcelReaderUtil.rowToObj(beanName, sheet.getRow(i), clazz);
                result.add(o);
            }
        }
        countDownLatch.countDown();
        log.info("ExcelReaderThread {} end,benginRowNumber {},endRowNumber {},countDownLatch {}", Thread.currentThread().getName(), benginRowNumber, endRowNumber, countDownLatch.getCount());
        return result;
    }
}
